package com.lookoutthebush.companyindex.util;

import com.lookoutthebush.companyindex.dtos.UsersDTO;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

/**
 * @author LooKOut
 */
public class UserManager {

    public static UsersDTO getUser() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user == null) {
            return null;
        }
        UsersDTO usersDTO = new UsersDTO();
        usersDTO.setUsername(user.getUsername());
        usersDTO.setRole(user.getAuthorities().stream().findFirst().get().getAuthority());
        return usersDTO;
    }
}
